﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ManageStore.IViews;

namespace ManageStore.Forms
{
    public partial class FrmMain : Form, IMainView
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        Dictionary<string, Form> LstFrom = new Dictionary<string, Form>();

        public void ShowForm(string pFormName, Func<object> pFncCreateForm)
        {
            Form oForm;
            if (LstFrom.Keys.Contains(pFormName))
            {
                oForm = LstFrom[pFormName] as Form;
                oForm.WindowState = FormWindowState.Maximized;
                oForm.Activate();
                return;
            }
            oForm = pFncCreateForm() as Form;
            if (oForm != null)
            {
                LstFrom.Add(pFormName, oForm);
                oForm.MdiParent = this;
                oForm.Show();
                oForm.Disposed += ((send, ea) =>
                {
                    if (LstFrom.Keys.Contains(pFormName))
                        LstFrom.Remove(pFormName);
                });
            }
        }

        private void loạiHàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LstFrom.Keys.Contains("FrmProductType"))
            {
                (LstFrom["FrmProductType"] as FrmProductType).Activate();
                return;
            }
            FrmProductType _FrmProductType = new FrmProductType();
            LstFrom.Add("FrmProductType", _FrmProductType);
            _FrmProductType.MdiParent = this;
            _FrmProductType.Show();
            _FrmProductType.Disposed += ((send, ea) =>
            {
                if (LstFrom.Keys.Contains("FrmProductType"))
                    LstFrom.Remove("FrmProductType");
            });
        }

        private void hàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LstFrom.Keys.Contains("FrmProduct"))
            {
                (LstFrom["FrmProduct"] as FrmProduct).Activate();
                return;
            }
            FrmProduct _FrmProduct = new FrmProduct();
            LstFrom.Add("FrmProduct", _FrmProduct);
            _FrmProduct.MdiParent = this;
            _FrmProduct.Show();
            _FrmProduct.Disposed += ((send, ea) =>
                {
                    if (LstFrom.Keys.Contains("FrmProduct"))
                        LstFrom.Remove("FrmProduct");
                });
        }

        private void nhậpHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LstFrom.Keys.Contains("FrmBuy"))
            {
                (LstFrom["FrmBuy"] as FrmBuy).Activate();
                return;
            }
            FrmBuy _FrmBuy = new FrmBuy();
            LstFrom.Add("FrmBuy", _FrmBuy);
            _FrmBuy.MdiParent = this;
            _FrmBuy.Show();
            _FrmBuy.Disposed += ((send, ea) =>
            {
                if (LstFrom.Keys.Contains("FrmBuy"))
                    LstFrom.Remove("FrmBuy");
            });
        }

        private void bánHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LstFrom.Keys.Contains("FrmSale"))
            {
                (LstFrom["FrmSale"] as FrmSale).Activate();
                return;
            }
            FrmSale _FrmSale = new FrmSale();
            LstFrom.Add("FrmSale", _FrmSale);
            _FrmSale.MdiParent = this;
            _FrmSale.Show();
            _FrmSale.Disposed += ((send, ea) =>
            {
                if (LstFrom.Keys.Contains("FrmSale"))
                    LstFrom.Remove("FrmSale");
            });
        }

        private void nhàCungCấpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LstFrom.Keys.Contains("FrmSupplier"))
            {
                (LstFrom["FrmSupplier"] as FrmSupplier).Activate();
                return;
            }
            FrmSupplier _FrmSupplier = new FrmSupplier();
            LstFrom.Add("FrmSupplier", _FrmSupplier);
            _FrmSupplier.MdiParent = this;
            _FrmSupplier.Show();
            _FrmSupplier.Disposed += ((send, ea) =>
            {
                if (LstFrom.Keys.Contains("FrmSupplier"))
                    LstFrom.Remove("FrmSupplier");
            });
        }

        private void đơnVịTínhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LstFrom.Keys.Contains("FrmUnit"))
            {
                (LstFrom["FrmUnit"] as FrmUnit).Activate();
                return;
            }
            FrmUnit _FrmUnit = new FrmUnit();
            LstFrom.Add("FrmUnit", _FrmUnit);
            _FrmUnit.MdiParent = this;
            _FrmUnit.Show();
            _FrmUnit.Disposed += ((send, ea) =>
            {
                if (LstFrom.Keys.Contains("FrmUnit"))
                    LstFrom.Remove("FrmUnit");
            });
        }

        private void báoCáoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LstFrom.Keys.Contains("FrmReport"))
            {
                (LstFrom["FrmReport"] as FrmReport).Activate();
                return;
            }
            Form _Frm = new FrmReport();
            LstFrom.Add("FrmReport", _Frm);
            _Frm.MdiParent = this;
            _Frm.Show();
            _Frm.Disposed += ((send, ea) =>
            {
                if (LstFrom.Keys.Contains("FrmReport"))
                    LstFrom.Remove("FrmReport");
            });
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //for (int i = 0; i < LstFrom.Count; i++)
            //{
            //    LstFrom.ElementAt(i--).Value.Close();
            //}
        }
    }
}
